const initBlog = async() => {
    await $.ajax({
        url:'https://wheatblog.asia/API/getFanCount.php',
        type:'get',
        dataType:'json',
        success:function(data){
            $('.fanCount')[0].innerText = data
        },
        fail:function(status){
            alert(status)
        }
    })
    // 头像和姓名
    await $.ajax({
        url:'https://so.csdn.net/api/v3/search?q=m0_56223760&t=userinfo',
        type:'get',
        dataType:'json',
        success:function(data){
            $('.name')[0].innerText = data.result_vos[0].nickname
            $('.xwcms')[0].src = data.result_vos[0].user_pic
        },
        fail:function(status){
            alert(status)
        }
    })
    await getAllArticle()
    $('.blog')[0].innerText = articleNum;
        // 将文章数据整理分类
    var blogList = articleList
    let classifyArr = []
    //    以——分割提取前部分字符存进classifyArr
    blogList.forEach(item => classifyArr.push(item.title.split('——')[0]))
    //    提取有两个及以上相同的
    classifyArr = classifyArr.filter((item,index,arr) => arr.indexOf(item) !== index)
    //    去除数组中值为空的
    classifyArr = classifyArr.filter(item => item)
    //    过滤数组中相同的
    classifyArr = Array.from(new Set(classifyArr))
    classifyArr.push('其他')
    $('.classify')[0].innerText = classifyArr.length
    let blogArticle = {}
    classifyArr.forEach((item,index) => blogArticle[index] = {name:item,article:[]})
    //    从bloglist中筛选出classifyArr开头相同的并将不同的存进其他数组
    blogList.forEach((item,index) => {
        if (classifyArr.some(classify => item.title.startsWith(classify))) {
            classifyArr.forEach(data =>{
                item.title.startsWith(data)
                    ? blogArticle[classifyArr.indexOf(data)].article.push(item)
                    : ''
            })
        }else{
            blogArticle[Object.keys(blogArticle).length - 1].article.push(item)
        }
    });
    // 渲染文章列表
        let tmpHtml = ``
    for(let i in blogArticle){
            // console.log(blogArticle);
            tmpHtml += `<div class="group">
            <span class="groupName">${blogArticle[i].name}</span>
            <div class="content" style="margin-top: 20px;">`
        blogArticle[i].article.forEach(item => {
                tmpHtml += `<div class="item">
                                <a href="${item.url}" style="text-decoration:none;">
                                <div class="title">
                                ${item.title}
                                </div>
                                <div class="pimg">
                                    <img src="${item.picList[0]}" alt="">
                                </div>
                                </a>
                                <div class="desc">${item.description}</div>
                                <div class="myTag">
                                    <span class="tag">阅读量：${item.viewCount}</span>
                                    <span class="tag">点赞：${item.diggCount}</span>
                                    <span class="tag">收藏：${item.collectCount}</span>
                                </div>
                                <div class="info">
                                    <div>作者:蜡笔不新</div>
                                    <div>${item.postTime}</div>
                                </div>
                            </div>`
        })
        tmpHtml += `</div></div>`
    }
    //    根据blogArticle[i].article.viewCount从大到小排序
    blogList.sort(function(a,b){
        return b.viewCount - a.viewCount
    })
    let rankingHtml = ''
    blogList.forEach((item,index)=>{
            rankingHtml += `<li class="ranking-item">
                                <a href="${item.url}" target="_blank" rel="nofollow">
                                ${index+1}.&nbsp${item.title}<img src="https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png" alt="">
                                <span class="read">${item.viewCount}</span>
                                </a>
                            </li>`
    })
    $('.ranking-list')[0].innerHTML = rankingHtml
    $('.group-content')[0].innerHTML += tmpHtml
    let iframe = $('#loding')[0]
    iframe.parentNode.removeChild(iframe);
    $('#app')[0].style.display = 'block'
}

initBlog()

let articleNum;
let articleList = []
let paramsNum = 1
// 获取所有文章
async function getAllArticle() {
    await $.ajax({
        type: 'GET',
        dataType: 'json',
        data:{
            page:paramsNum,
            size:20,
            businessType:'blog',
            noMore:false,
            username:'m0_56223760'
        },
        url: 'https://blog.csdn.net/community/home-api/v1/get-business-list',
        success: function(res){//请求成功后的回调函数
            articleNum = res.data.total
            res.data.list.forEach(item => {
                articleList.push(item)
            })
            if (articleNum !== articleList.length) {
                getAllArticle(paramsNum++)
            }
            console.log(articleList);
        },
        fail: function(status){
            console.log(status)
        }
    })
}

function extractCommonPrefix(str1, str2) {
    let i = 0; // 初始化索引为0
    
    while (i < Math.min(str1.length, str2.length)) {
        if (str1[i] !== str2[i]) {
            break; // 如果当前位置不相等，则跳出循环
        }
        
        i++; // 递增索引
    }
    
    return str1.substring(0, i); // 返回从第0个位置到索引i之间的子字符串作为结果
}
